/**
 * Created by tz on 2018/12/27.
 */
import React, { Component } from 'react';
import {Form, Card, Button, Input, Popconfirm, message} from 'antd';
import CountryService from '../../service/CountryService';
export default class CountryEdit extends Component {
    state = {
        title:'Country新增',
        tableData:[],
        loading:false,
        formItems: [
            {name: '中文名称', type: 'input', fieldName: 'cnName', required: true},
            {name: '英文名称', type: 'input', fieldName: 'enName', required: true},
            {name: '简称', type: 'input', fieldName: 'shortName', required: true}
        ],
        error:false,
        data:{}
    };
    constructor(props) {
        super(props)
    }

    componentDidMount() {
        let id = this.props.match.params.id;
        if(id){
            this.setState({title:'Country修改'});
            this.fetch(id);
        }
    }

    fetch(id){
        this.setState({
            loading: true
        });
        CountryService.getOne(id).done((res)=>{
            console.log(res.entity);
            this.setState({data:res.entity,loading: false})
        })
    }

    //页面check
    check(){
        this.state.error = false;

        //中文名称check
        if (this.state.data.cnName === null || this.state.data.cnName === '' || typeof this.state.data.cnName === 'undefined') {
            this.state.error = true;
            message.error("请输入中文名称！");
            return
        }
        if (this.state.data.cnName.length > 255) {
            this.state.error = true;
            message.error("中文名称最多只能输入255个文字！");
            return
        }

        //英文名称check
        if (this.state.data.enName === null || this.state.data.enName === '' || typeof this.state.data.enName === 'undefined') {
            this.state.error = true;
            message.error("请输入英文名称！");
            return
        }
        if (this.state.data.enName.length > 255) {
            this.state.error = true;
            message.error("英文名称最多只能输入255个文字！");
            return
        }

        //简称check
        if (this.state.data.shortName === null || this.state.data.shortName === '' || typeof this.state.data.shortName === 'undefined') {
            this.state.error = true;
            message.error("请输入简称！");
            return
        }
        if (this.state.data.shortName.length > 255) {
            this.state.error = true;
            message.error("简称最多只能输入255个文字！");
            return
        }
    }

    save(){
        //页面check
        this.check();

        if (this.state.error === false) {
            let id = this.props.match.params.id;
            if (id) {
                CountryService.updateOne(this.state.data).done((res) => {
                    console.log(res);
                    this.props.history.goBack();
                })
            } else {
                CountryService.addOne(this.state.data).done((res) => {
                    console.log(res);
                    this.props.history.goBack();
                })
            }
        }
    }

    //页面渲染
    render() {
        return (
            <Card title={this.state.title}>
                <Form>
                    {this.state.formItems.map((item,i)=>{
                        if(item.type==='input'){
                            return(
                                <Form.Item
                                    label={item.name}
                                    key={i}
                                >
                                    <Input placeholder={"请输入"+item.name} value={this.state.data[item.fieldName]} onChange={(e)=>{
                                        this.state.data[item.fieldName] = e.target.value;
                                        this.forceUpdate();
                                    }}/>
                                </Form.Item>
                            )
                        }
                    })}
                    <Form.Item>
                        <Popconfirm placement="top" okText="确认" cancelText="关闭" title="是否确定保存?" onConfirm={() =>{
                            this.save();
                        }}>
                            <Button type="primary" icon="save">保存</Button>
                        </Popconfirm>

                    </Form.Item>
                </Form>
            </Card>
        );
    }
}